<template>
  <div class="login">
    <!-- 头部 -->
    <div class="top">
      <van-nav-bar>
        <template #left>
          <i class="iconfont iconbtn_nav_back mylefticon"></i>
        </template>
      </van-nav-bar>
    </div>
    <!-- 内容 -->
    <div class="content">
      <h2>您好,请登录</h2>
      <van-form ref="myform">
        <van-field
          v-model="use.mobile"
          placeholder="请输入电话号码"
          :rules="rules.mobile"
          name="mobile"
        >
          <template #left-icon>
            <i class="iconfont iconbianzu1"></i>
          </template>
        </van-field>
        <van-field
          v-model="use.code"
          placeholder="亲输入验证码"
          :rules="rules.code"
          ><template #button>
            <span class="myColor" @click="getCode">{{
              isBack ? time + 's后重启' : '发送验证码'
            }}</span>
          </template>
          <template #left-icon>
            <i class="iconfont iconyanzhengma"></i>
          </template>
        </van-field>
        <p class="p1">
          登录即同意<span class="myColor">《用户使用协议》</span>和
          <span class="myColor">《隐私协议》</span>
        </p>
        <div class="loginBtn">
          <van-button round block type="danger" native-type="submit">
            登录
          </van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { apiGetCode } from '@/api/login'
export default {
  data () {
    return {
      use: {
        mobile: '18511111111',
        code: ''
      },
      rules: {
        mobile: [
          { required: true, message: '请填写电话号码', trigger: 'onBlur' },
          {
            pattern: /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '请输入正确手机号码',
            trigger: 'onBlur'
          }
        ],
        code: [
          { required: true, message: '请填写验证码', trigger: 'onBlur' },
          {
            pattern: /^\d{4}$/,
            message: '请输入正确验证码',
            trigger: 'onBlur'
          }
        ]
      },
      time: 5,
      temer: null,
      isBack: false
    }
  },
  methods: {
    async getCode () {
      if (this.isBack) {
        return
      }
      this.isBack = true
      this.timer = setInterval(() => {
        this.time--
        if (this.time < 0) {
          clearInterval(this.timer)
          this.isBack = false
          this.time = 5
        }
      }, 1000)
      await this.$refs.myform.validate('mobile')
      this.$toast.loading({
        duration: 0, // 一直显示
        message: '加载中...',
        forbidClick: true
      })

      const resCode = await apiGetCode(this.use.mobile)

      this.$toast.success(resCode.data)
    }
  }
}
</script>

<style lang="less" scope>
.login {
  font-size: 12px;
  /deep/ .van-nav-bar__left {
    padding-left: 0;
  }
  .mylefticon {
    font-size: 44px;
  }
  .content {
    padding: 0 15px;
    h2 {
      margin: 50px 0 63px 0;
    }
  }
  .myColor {
    color: #13bdd7;
  }
  .p1 {
    margin: 15px 0 12px 0;
  }
  .loginBtn {
    margin-top: 44px;
  }
}
</style>
